#include "drawableObject.h"


DrawableObject::DrawableObject(ID3D10Device * device)
{
	m_pd3dDevice=device;
	m_Position=D3DXVECTOR3(0.0f,0.0f,0.0f);
}

void DrawableObject::CreateVertexBuffer(SimpleVertex * vertices, int n)
{
	if(!m_pd3dDevice) return;

	D3D10_BUFFER_DESC bd;
	bd.Usage = D3D10_USAGE_DEFAULT;
	bd.ByteWidth = sizeof(SimpleVertex)*n;
	bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;
	D3D10_SUBRESOURCE_DATA InitData;
	InitData.pSysMem = vertices;


	m_pd3dDevice->CreateBuffer( &bd, &InitData, &m_pVertexBuffer );
	m_vertexBufferSize=n;

}

void DrawableObject::CreateIndexBuffer( DWORD * indices, int n ,D3D10_PRIMITIVE_TOPOLOGY topology)
{
	if(!m_pd3dDevice) return;

	m_Topology=topology;

	D3D10_BUFFER_DESC bd;


	D3D10_SUBRESOURCE_DATA InitData;
	//Index buffer
	bd.Usage = D3D10_USAGE_DEFAULT;
	bd.ByteWidth = sizeof( DWORD ) * n;
	bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;
	InitData.pSysMem = indices;

	m_pd3dDevice->CreateBuffer( &bd, &InitData, &m_pIndexBuffer );
	m_indexBufferSize=n;


}

DrawableObject::~DrawableObject()
{
	if(m_pIndexBuffer) m_pIndexBuffer->Release();
	if(m_pVertexBuffer) m_pVertexBuffer->Release();

}

void DrawableObject::Draw()
{
	if(!m_pVertexBuffer||!m_pIndexBuffer) return;

	// Set vertex buffer
	UINT stride = sizeof( SimpleVertex );
	UINT offset = 0;
	m_pd3dDevice->IASetVertexBuffers( 0, 1, &m_pVertexBuffer, &stride, &offset );

	// Set index buffer
	m_pd3dDevice->IASetIndexBuffer( m_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );

	// Set primitive topology
	m_pd3dDevice->IASetPrimitiveTopology( m_Topology );

	m_pd3dDevice->DrawIndexed(m_indexBufferSize, 0, 0 );

}

